package com.jichangxiu.framework.utils;

import cn.hutool.core.collection.ListUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;

public class AopUtils {

    public static String getClassName(ProceedingJoinPoint proceedingJoinPoint) {
        return proceedingJoinPoint.getTarget().getClass().getName();
    }

    public static String getMethodName(ProceedingJoinPoint proceedingJoinPoint) {
        return proceedingJoinPoint.getSignature().getName();
    }

    public static List<Object> getParameterArguments(ProceedingJoinPoint proceedingJoinPoint) {
        return ListUtil.toList(proceedingJoinPoint.getArgs());
    }

    public static Annotation getAnnotation(ProceedingJoinPoint proceedingJoinPoint, Class<? extends Annotation> annotationClass) {
        List<Annotation> annotationList = getAnnotationList(proceedingJoinPoint);
        for (Annotation annotation : annotationList)
            if (annotation.annotationType().equals(annotationClass))
                return annotation;
        return null;
    }

    public static List<Annotation> getAnnotationList(ProceedingJoinPoint proceedingJoinPoint) {
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = signature.getMethod();
        return ListUtil.toList(method.getAnnotations());
    }

}
